package cn.senjoeson.triper.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.activity.DinningListActivity;
import cn.senjoeson.triper.activity.ThingsToDoActivity;
import cn.senjoeson.triper.adapter.SearchTagAdapter;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.HistoryBean;
import cn.senjoeson.triper.dao.HistoryBeanDao;
import cn.senjoeson.triper.view.CustomLinearlayout;

/**
 * Created by senjoeson on 2018/2/10.
 */

public class SearchHistoryFragment extends Fragment implements View.OnClickListener {
    @BindView(R.id.flowlayout)
    TagFlowLayout mFlowlayout;
    @BindView(R.id.foodrink)
    CustomLinearlayout mFoodrink;
    Unbinder unbinder;
    @BindView(R.id.thingstodo)
    CustomLinearlayout mThingstodo;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.serarch_history, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        mFoodrink.setOnClickListener(this);
        mThingstodo.setOnClickListener(this);
        HistoryBeanDao historyBeanDao = GreenDaoManager.getInstance().getNewSession().getHistoryBeanDao();
        List<HistoryBean> list = historyBeanDao.queryBuilder().orderDesc(HistoryBeanDao.Properties.CurrentSystemMills).build().list();
        if (list != null) {
            mFlowlayout.setAdapter(new SearchTagAdapter(list));
        } else {
            mFlowlayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.foodrink:
                startActivity(new Intent(getContext(), DinningListActivity.class));
                break;
            case R.id.thingstodo:
                startActivity(new Intent(getContext(), ThingsToDoActivity.class));
                break;
        }
    }
}
